//
//  PartySimpleToUnderstandVC.m
//  homeland
//
//  Created by caesar on 14-8-12.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//

#import "PartySimpleToUnderstandVC.h"
#import "RATreeView.h"
#import "RADataObject.h"
#import "PartyImageDetailViewController.h"
#import "FeeManagementViewController.h"
#import "FeeCalculatorViewController.h"
#import "ListViewController.h"

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
@interface PartySimpleToUnderstandVC ()

@property (strong, nonatomic) NSArray *data;
@property (strong, nonatomic) id expanded;
@property (weak, nonatomic) RATreeView *treeView;

@end

@implementation PartySimpleToUnderstandVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"党务一点通";
   
    //我要入党
    RADataObject *rd1 = [RADataObject dataObjectWithName:@"积极分子培养流程" children:nil];
    RADataObject *rd2 = [RADataObject dataObjectWithName:@"接收预备党员流程" children:nil];
    RADataObject *rd3 = [RADataObject dataObjectWithName:@"入党宣誓流程" children:nil];
    RADataObject *rd4 = [RADataObject dataObjectWithName:@"预备党员转正流程" children:nil];
    
    RADataObject *rd = [RADataObject dataObjectWithName:@"我要入党"
                                                  children:[NSArray arrayWithObjects:rd1, rd2, rd3, rd4, nil]];

//    self.expanded = notebook1;
    //我要转组织关系
    RADataObject *wyzzzgx1 = [RADataObject dataObjectWithName:@"我要转组织关系" children:nil];
    RADataObject *wyzzzgx = [RADataObject dataObjectWithName:@"我要转组织关系"
                                               children:[NSArray arrayWithObjects:wyzzzgx1,  nil]];
    
    //我要缴纳党费
    RADataObject *wyjndf1 = [RADataObject dataObjectWithName:@"我要缴纳党费" children:nil];
    RADataObject *wyjndf2 = [RADataObject dataObjectWithName:@"党费收缴、使用和管理" children:nil];
    RADataObject *wyjndf3 = [RADataObject dataObjectWithName:@"党费缴纳计算器" children:nil];
 
    RADataObject *wyjndfx = [RADataObject dataObjectWithName:@"我要缴纳党费"
                                                    children:[NSArray arrayWithObjects:wyjndf1,wyjndf2,wyjndf3,  nil]];
    //我要参加组织活动
    RADataObject *wycjzzhd1 = [RADataObject dataObjectWithName:@"我要参加组织活动" children:nil];
    RADataObject *wycjzzhd = [RADataObject dataObjectWithName:@"我要参加组织活动"
                                                    children:[NSArray arrayWithObjects:wycjzzhd1,  nil]];
 
    
    
    self.data = [NSArray arrayWithObjects:rd, wyzzzgx, wyjndfx, wycjzzhd, nil];
    
    RATreeView *treeView = [[RATreeView alloc] initWithFrame:self.view.frame];
    
    treeView.delegate = self;
    treeView.dataSource = self;
    treeView.separatorStyle = RATreeViewCellSeparatorStyleSingleLine;
    
    [treeView reloadData];
//    [treeView expandRowForItem:phone withRowAnimation:RATreeViewRowAnimationLeft]; //expands Row
    [treeView setBackgroundColor:UIColorFromRGB(0xF7F7F7)];
    
    self.treeView = treeView;
    [self.view addSubview:treeView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if([[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."][0] intValue] >= 7) {
//        CGRect statusBarViewRect = [[UIApplication sharedApplication] statusBarFrame];
//        float heightPadding = statusBarViewRect.size.height+self.navigationController.navigationBar.frame.size.height;
//        self.treeView.contentInset = UIEdgeInsetsMake(heightPadding, 0.0, 0.0, 0.0);
//        self.treeView.contentOffset = CGPointMake(0.0, -heightPadding);
    }
    self.treeView.frame = self.view.bounds;
}

#pragma mark TreeView Delegate methods
- (CGFloat)treeView:(RATreeView *)treeView heightForRowForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
{
    return 47;
}

- (NSInteger)treeView:(RATreeView *)treeView indentationLevelForRowForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
{
    return 3 * treeNodeInfo.treeDepthLevel;
}

- (BOOL)treeView:(RATreeView *)treeView shouldExpandItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
{
    return YES;
}
//如果有子项，展开
- (BOOL)treeView:(RATreeView *)treeView shouldItemBeExpandedAfterDataReload:(id)item treeDepthLevel:(NSInteger)treeDepthLevel
{
    
    
    //  if ([item isEqual:self.expanded]) {
    //    return YES;
    //  }
    return NO;
}
//cell背景
- (void)treeView:(RATreeView *)treeView willDisplayCell:(UITableViewCell *)cell forItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
{
    if (treeNodeInfo.treeDepthLevel == 0) {
        cell.backgroundColor = UIColorFromRGB(0xF7F7F7);
    } else if (treeNodeInfo.treeDepthLevel == 1) {
        cell.backgroundColor = UIColorFromRGB(0xD1EEFC);
    } else if (treeNodeInfo.treeDepthLevel == 2) {
        cell.backgroundColor = UIColorFromRGB(0xE0F8D8);
    }
}

#pragma mark TreeView Data Source

- (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
{
    NSInteger numberOfChildren = [treeNodeInfo.children count];
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    //  cell.detailTextLabel.text = [NSString stringWithFormat:@"Number of children %d", numberOfChildren];
    if (numberOfChildren > 0) {
        UIImage *image = [UIImage imageNamed:@"menu_icon_1.png"];
        cell.imageView.image=image;
    }else{
       [cell.textLabel setTextColor:[UIColor orangeColor]];
        
    }
    cell.textLabel.text = ((RADataObject *)item).name;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (treeNodeInfo.treeDepthLevel == 0) {
        cell.detailTextLabel.textColor = [UIColor blackColor];
    }
    return cell;
}
//子项cell数量
- (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item
{
    if (item == nil) {
        return [self.data count];
    }
    RADataObject *data = item;
    return [data.children count];
}

- (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item
{
    
    RADataObject *data = item;
    if (item == nil) {
        NSLog(@"child  tem == nil index=%d",index);
        return [self.data objectAtIndex:index];
    }
    
    NSLog(@"child  tem != nil index=%d",index);
    return [data.children objectAtIndex:index];
}

//在这里跳转
- (void)tableView:(UITableView *)tableView didSelectRowJumpAtisExpanded:(BOOL *)expanded cellName:(NSString*)cellName
{

 
    
    NSLog(@"didSelectRowAtIndexPath cellName=%@",cellName);
    //  我要入党 / 我要转组织关系
    if (
        [cellName isEqualToString:@"积极分子培养流程"] ||
        [cellName isEqualToString:@"接收预备党员流程"] ||
        [cellName isEqualToString:@"入党宣誓流程"]    ||
        [cellName isEqualToString:@"预备党员转正流程"] ||
        [cellName isEqualToString:@"我要转组织关系"]  ||
        [cellName isEqualToString:@"我要缴纳党费"]
        ) {
 
        PartyImageDetailViewController *detailVC = [[PartyImageDetailViewController alloc]initWithNibName:@"PartyImageDetailViewController" bundle:nil];
        NSDictionary *dic = @{@"name":cellName};
        
        detailVC.config = dic;
        [self.navigationController pushViewController:detailVC animated:YES];
    }else if ([cellName isEqualToString:@"党费收缴、使用和管理"]){
        FeeManagementViewController *detailVC = [[FeeManagementViewController alloc]initWithNibName:@"FeeManagementViewController" bundle:nil];
        [self.navigationController pushViewController:detailVC animated:YES];
    }else if ([cellName isEqualToString:@"党费缴纳计算器"]){
        FeeCalculatorViewController *detailVC = [[FeeCalculatorViewController alloc]initWithNibName:@"FeeCalculatorViewController" bundle:nil];
        [self.navigationController pushViewController:detailVC animated:YES];
    }else if ([cellName isEqualToString:@"我要参加组织活动"]){
        ListViewController *detailVC = [[ListViewController alloc]initWithNibName:@"ListViewController" bundle:nil];
        detailVC.config = @{@"key":@"志愿最新鲜"};
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}

@end
